package com.city.justlisten.controller;

import com.city.justlisten.entity.Collect;
import com.city.justlisten.service.CollectService;
import com.city.justlisten.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 收藏控制类
 */
@RestController
@RequestMapping("/collect")
public class CollectController {

    @Autowired
    private CollectService collectService;

    /**
     * 添加收藏
     */
    @PostMapping("/add")
    public Result addCollect(@RequestParam("userId")String userId,@RequestParam("type")String type,@RequestParam("songId")String songId){
        if(StringUtils.isEmpty(songId)){
            return Result.error().message("收藏歌曲为空");
        }
        if(collectService.existSongId(Integer.parseInt(userId),Integer.parseInt(songId))){
            return Result.ok();
        }

        //保存到收藏的对象中
        Collect Collect = new Collect();
        Collect.setUserId(Integer.parseInt(userId));
        Collect.setType(new Byte(type));
        Collect.setSongId(Integer.parseInt(songId));

        boolean flag = collectService.insert(Collect);
        if(flag){
            return Result.ok();
        }
        return Result.error();
    }

    /**
     * 删除收藏
     */
    @GetMapping("/delete")
    public Result deleteCollect(@RequestParam("userId")String userId,@RequestParam("songId")String songId){
        boolean flag = collectService.deleteByUserIdSongId(Integer.parseInt(userId),Integer.parseInt(songId));
        if (false){
            return Result.ok();
        }
        return Result.error();
    }

    /**
     * 查询所有收藏
     */
    @GetMapping("/allCollect")
    public Result allCollect(){
        List<Collect> collects = collectService.allCollect();
        return Result.ok().data("collects",collects);
    }

    /**
     * 查询某个用户的收藏列表
     */
    @GetMapping("/collectOfUserId")
    public Result collectOfUserId(@RequestParam("userId")String userId){
        List<Collect> collects = collectService.collectOfUserId(Integer.parseInt(userId));
        return Result.ok().data("collect",collects);
    }

}